package com.jial08.rabbitmqretry.component;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * <p>类描述：消费消息的失败处理。</p>
 *
 * @author jl
 * @version v1.0.0.1。
 * @since JDK1.8。
 * <p>创建日期：2020/12/8 下午10:32。</p>
 */
@Component
@Slf4j
public class FaildConsumer {

	@RabbitListener(queues = "mall.pay.failed")
	public void sendMail(Message message, Map<String, Object> map, Channel channel) throws IOException {
		String receivedExchange = message.getMessageProperties().getReceivedExchange();
		log.info("receivedExchange: {}", receivedExchange);
		try {
			log.warn("消费失败处理：{}", map);
		} finally {
			/**
			 * 无论消费成功还是消费失败,都要手动进行ack,因为即使消费失败了,也已经将消息重新投递到重试队列或者失败队列
			 * 如果不进行ack,生产者在超时后会进行消息重发,如果消费者依然不能处理，则会存在死循环
			 */
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		}
	}

}
